package com.neuedu.studentmanage.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.studentmanage.entity.Pager;
import com.neuedu.studentmanage.entity.Result;
import com.neuedu.studentmanage.entity.Student;
import com.neuedu.studentmanage.intercepter.Const;
import com.neuedu.studentmanage.service.StudentService;

@Controller
public class StudentController {

	@Autowired
	private StudentService studentService;

	@RequestMapping("student/index")
	public String studentIndex() {
		// 首页只打开页面，不加载数据;页面的数据使用在easy ui中通过ajax技术进行异步加载的
		return "admin/studentindex";
	}

	// easyui请求时使用的是post方法，这里需要使用post方法
	// easy ui进行分页查询时，使用page传递查询的页码，使用rows传递查询的记录数
	@RequestMapping("admin/studentindex")
	@ResponseBody
	public Pager<Student> studentList(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer rows) {
		Pager<Student> pager = new Pager<>(page, rows);
		// 根据查询条件进行分页查询
		List<Student> classroomList = studentService.findListByPager(pager);

		Integer total = studentService.findTotalByPager(pager);

		// 把查询到数据保存到pager对象中，再转换成json返回给easy ui
		pager.setRows(classroomList);
		pager.setTotal(total);

		return pager;
	}

	@RequestMapping("student/add")
	@ResponseBody
	public Result doAdd(Student student) {
		Result result = studentService.add(student);
		return result;
	}

	@RequestMapping("student/edit")
	@ResponseBody
	public Result doEdit(Student student) {
		Result result = studentService.edit(student);
		return result;
	}

	@RequestMapping("student/delete")
	@ResponseBody
	public Result doDelete(Integer stuId) {

		List<Student> list = new ArrayList<Student>();
		list.add(new Student(stuId));
		Result result = studentService.deleteByIds(list);
		return result;
	}

	@RequestMapping("student/all")
	@ResponseBody
	public List<Student> findAll() {

		return studentService.findAll();
	}

	@RequestMapping("menu/index")
	public String studentInfoIndex() {
		// 首页只打开页面，不加载数据;页面的数据使用在easy ui中通过ajax技术进行异步加载的
		return "student/stuInfo/stuinfo";
	}

	@RequestMapping("student/stuInfo/stuinfo")
	@ResponseBody
	public Pager<Student> studentInfo(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer rows, HttpServletRequest request) {
		Pager<Student> pager = new Pager<>(page, rows);
		// 根据查询条件进行分页查询
		int stu = (int) request.getSession().getAttribute(Const.SESSION_USER);
		Student student = studentService.findById(stu);
		List<Student> listStudent = new ArrayList<>();
		listStudent.add(student);

		// 把查询到数据保存到pager对象中，再转换成json返回给easy ui
		pager.setRows(listStudent);
		pager.setTotal(1);

		return pager;
	}

}
